package com.example.rain.threadtest;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

public class MyService extends Service {
    private Handler handler;
    private int progress;

    public MyService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                progress = msg.getData().getInt("pro");
            }
        };
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        new Thread() {
            @Override
            public void run() {

                for (int i = 0; i < 100; i++) {
                        try {
                            Thread.sleep(30);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        Message msg = new Message();
                        Bundle bd = new Bundle();
                        msg.setData(bd);
                        bd.putInt("pro", i);
                        handler.sendMessage(msg);

                }
            }

        }.start();
        return super.onStartCommand(intent, flags, startId);
    }

    public class Myblind extends Binder {

        public int getProgress() {
            return progress;
        }

    }

    @Override
    public IBinder onBind(Intent intent) {
        return new Myblind();
    }

}
